// pages/admin/user/edit/admin_user_edit.js
const AdminBiz = require('../../../../biz/admin_biz.js');
const pageHelper = require('../../../../helper/page_helper.js');
const cloudHelper = require('../../../../helper/cloud_helper.js');
const validate = require('../../../../helper/validate.js');
const AdminUsersBiz = require('../../../../biz/admin_users_biz.js');
const timeHelper = require('../../../../helper/time_helper.js');
const dataHelper = require('../../../../helper/data_helper.js');
const setting = require('../../../../setting/setting.js');

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
        isLoad: false,
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function (options) {
        console.info("userDeit go on,options:"+JSON.stringify(options));
        if (!AdminBiz.isAdmin(this)) return;
        if (!pageHelper.getOptions(this, options,'openId')) return;

        console.info("userDeit go on,data:"+JSON.stringify(this.data));

        this._loadDetail();

	},

    /**
     * 加载初始数据
     */
	_loadDetail: async function () {
		if (!AdminBiz.isAdmin(this)) return;
        let openId = this.data.openId;
        // console.info("openId:"+openId);
		if (!openId) return;

		if (!this.data.isLoad) this.setData(await AdminUsersBiz.initFormData(openId)); // 初始化表单数据

		let params = {
			openId
		};
		let opt = {
			title: 'bar'
		};
		let user = await cloudHelper.callCloudData('admin/user_detail', params, opt);
        
        // console.info("user:"+JSON.stringify(user));
        if (!user) {
			this.setData({
				isLoad: null
			})
			return;
		};


		this.setData({
			isLoad: true,
            formName: user.USER_NAME,
            formMobile: user.USER_MOBILE,
            formCity: user.USER_CITY,
            formWork: user.USER_WORK,
            formTrade: user.USER_TRADE,
            formValidityDateStart: user.USER_VALIDITY_DATE_START,
            formValidityDateEnd: user.USER_VALIDITY_DATE_END
        });
        
        console.info("userDeit go on,data:"+JSON.stringify(this.data));
    },
    
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

    },
    
    bindValidityDateStartChange: function(e) {
        this.setData({
            formValidityDateStart: e.detail.value
        })
    },
     
    bindValidityDateEndChange: function(e) {
        this.setData({
            formValidityDateEnd: e.detail.value
        })
    },

	/** 
	 * 数据提交
	 */
	bindFormSubmit: async function () {
		if (!AdminBiz.isAdmin(this)) return;

        // let data = this.data;
        // console.info("data:"+JSON.stringify(data));
		// // 数据校验  by 类型
		// data = validate.check(data, AdminUsersBiz.CHECK_FORM, this);
		
		// try {
		// 	let newsId = this.data.id;
		// 	data.id = newsId;

		// 	let callback = async () => {

		// 		// 更新列表页面数据
		// 		let node = {
		// 			'NEWS_ORDER': data.formValidityDateStart,
		// 			'NEWS_TYPE': data.formValidityDateEnd
		// 		}
		// 		pageHelper.modifyPrevPageListNodeObject(newsId, node);

		// 		wx.navigateBack();

		// 	}
		// 	pageHelper.showSuccToast('修改成功', 2000, callback);

		// } catch (err) {
		// 	console.log(err);
		// }


        try {
            let data = this.data;
            console.info("data:"+JSON.stringify(data));

            let CHECK_FORM = {
                openId: 'openId|string',
                validityDateEnd: 'formValidityDateEnd|string|name=有效期-开始时间',
                validityDateStart: 'formValidityDateStart|string|name=有效期-结束时间'
            };
            // 数据校验 
            data = validate.check(data, CHECK_FORM, this);
            console.info("data:"+JSON.stringify(data));
            if (!data) return;

            let opts = {
                title: '提交中'
            }
            await cloudHelper.callCloudSumbit('admin/user_edit', data, opts).then(res => {
                let callback = () => {
                    wx.navigateBack();
                }
                pageHelper.showSuccToast('提交成功', 1500, callback);
            });
        } catch (err) {
            console.error(err);
        }
	},
})